package com.youlu.campus.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
 * campus官网 mongodb 配置
 *
 * @Description: MongoConfig
 * @Date 2025/7/17
 * @module MongoConfig
 * @author: su
 */
@Slf4j
@Configuration("websiteMongoConfig")
@EnableMongoRepositories(
        basePackages = "com.youlu.campus.website.repositories",
        mongoTemplateRef = "websiteMongoTemplate"
)
public class WebsiteMongoConfig {

    /**
     * campus官网 使用
     *
     * @param uri
     * @return
     */
    @ConditionalOnProperty(prefix = "spring.data.mongodb.website", name = "uri")
    @Bean(name = "websiteMongoDatabaseFactory")
    public MongoDatabaseFactory websiteMongoDatabaseFactory(@Value("${spring.data.mongodb.website.uri}") String uri) {
        SimpleMongoClientDatabaseFactory simpleMongoClientDatabaseFactory = new SimpleMongoClientDatabaseFactory(uri);
        return simpleMongoClientDatabaseFactory;
    }

    @Bean(name = "websiteMongoTemplate")
    public MongoTemplate websiteMongoTemplate(@Autowired(required = false) @Qualifier(value = "websiteMongoDatabaseFactory") MongoDatabaseFactory mongoDbFactory,
                                              @Autowired MongoDatabaseFactory defaultMongoDbFactory,
                                              MongoMappingContext context) {
        if (mongoDbFactory == null) {
            mongoDbFactory = defaultMongoDbFactory;
        }
        MappingMongoConverter converter = mappingMongoConverter(mongoDbFactory, context, null);
        return new MongoTemplate(mongoDbFactory, converter);
    }

    @Bean
    @ConditionalOnMissingBean(MappingMongoConverter.class)
    public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory mongoDbFactory, MongoMappingContext context, BeanFactory beanFactory) {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);

        mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
        try {
            mappingConverter.setCustomConversions(beanFactory.getBean(MongoCustomConversions.class));
        } catch (NoSuchBeanDefinitionException ex) {
            // Ignore
            log.error("mappingMongoConverter init error", ex);
        }
        return mappingConverter;
    }
}
